
package com.foxit.pdf2html5.web.common;

import java.io.Serializable;
import java.util.List;

public class Pager
	implements Serializable
{

	private long serialVersionUID;
	private int totalRows;
	private int pageSize;
	private int currentPage;
	private int totalPages;
	private int startRow;
	private boolean isGetCount;

	public boolean isGetCount()
	{
		return isGetCount;
	}

	public void setGetCount(boolean isGetCount)
	{
		this.isGetCount = isGetCount;
	}

	public Pager()
	{
		serialVersionUID = 23111L;
		pageSize = 10;
		isGetCount = true;
	}

	public Pager(int pageSize, int currentPage)
	{
		serialVersionUID = 23111L;
		this.pageSize = 10;
		isGetCount = true;
		this.pageSize = pageSize;
		this.currentPage = currentPage;
	}

	public Pager(int pageSize, int currentPage, int totalPage)
	{
		serialVersionUID = 23111L;
		this.pageSize = 10;
		isGetCount = true;
		this.pageSize = pageSize;
		this.currentPage = currentPage;
		totalPages = totalPage;
		isGetCount = false;
	}

	public Pager(int currentPage)
	{
		serialVersionUID = 23111L;
		pageSize = 10;
		isGetCount = true;
		this.currentPage = currentPage;
	}

	public int getStartRow()
	{
		return startRow;
	}

	public int getTotalPages()
	{
		return totalPages;
	}

	private void calTotalPages()
	{
		totalPages = totalRows % pageSize != 0 ? totalRows / pageSize + 1 : totalRows / pageSize;
	}

	public int getCurrentPage()
	{
		return currentPage;
	}

	public int getPageSize()
	{
		return pageSize;
	}

	public void setTotalRows(int totalRows)
	{
		this.totalRows = totalRows;
		calTotalPages();
	}

	public void setStartRow(int startRow)
	{
		this.startRow = startRow;
	}

	public void setTotalPages(int totalPages)
	{
		this.totalPages = totalPages;
	}

	public void setCurrentPage(int currentPage)
	{
		currentPage = currentPage >= 1 ? currentPage : 1;
		currentPage = currentPage <= totalPages ? currentPage : totalPages;
		startRow = (currentPage - 1) * pageSize;
		startRow = startRow >= 0 ? startRow : 0;
		this.currentPage = currentPage;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public int getTotalRows()
	{
		return totalRows;
	}

	public void first()
	{
		currentPage = 1;
		startRow = 0;
	}

	public void previous()
	{
		if (currentPage == 1)
		{
			return;
		} else
		{
			currentPage--;
			startRow = (currentPage - 1) * pageSize;
			return;
		}
	}

	public void next()
	{
		if (currentPage < totalPages)
			currentPage++;
		startRow = (currentPage - 1) * pageSize;
	}

	public void last()
	{
		currentPage = totalPages;
		startRow = (currentPage - 1) * pageSize;
	}

	public void refresh(int _currentPage)
	{
		currentPage = _currentPage;
		if (currentPage > totalPages)
			last();
	}

	public boolean isLastPage()
	{
		return currentPage == totalPages;
	}

	public boolean isFirstPage()
	{
		return currentPage == 1;
	}
}
